package veterinaria.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import veterinaria.excepcion.DAOExcepcion;
import veterinaria.modelo.Doctor;
import veterinaria.util.ConexionBD;

public class DoctorDAO extends BaseDAO {
	
	public Collection<Doctor> buscarPorNombre(String nombre)
			throws DAOExcepcion {
		String query = "select idDoctor,Usuarios_idusuario,dsApellido,dsNombre,dsCorreo,nuTeleFijo,nuTeleMovil from Doctor where stActivo = 'S' and dsNombre like ?";
		Collection<Doctor> lista = new ArrayList<Doctor>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nombre + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Doctor vo = new Doctor();
				vo.setIdDoctor(rs.getInt("idDoctor"));		
				vo.setUsuarios_idusuario(rs.getInt("usuarios_idusuario"));
				vo.setDsApellido(rs.getString("dsApellido"));
				vo.setDsNombre(rs.getString("dsNombre"));
				vo.setDsCorreo(rs.getString("dsCorreo"));
				vo.setNuTeleFijo(rs.getString("nuTeleFijo"));
				vo.setNuTeleMovil(rs.getString("nuTeleMovil"));
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}

	public Doctor insertar(Doctor vo) throws DAOExcepcion {
		String query = "insert into Doctor(idDoctor,Usuarios_idusuario,dsApellido,dsNombre,dsCorreo,nuTeleFijo,nuTeleMovil) values (?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, vo.getIdDoctor());
			stmt.setInt(2, vo.getUsuarios_idusuario());
			stmt.setString(3, vo.getDsApellido());
			stmt.setString(4, vo.getDsNombre());
			stmt.setString(5, vo.getDsCorreo());
			stmt.setString(6, vo.getNuTeleFijo());
			stmt.setString(7, vo.getNuTeleMovil());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			// Obtener el ultimo id
			//int id = 0;
			//query = "select last_insert_id()";
			//stmt = con.prepareStatement(query);
			//rs = stmt.executeQuery();
			//if (rs.next()) {
			//	id = rs.getInt(1);
			//}
			//vo.setIdDoctor(id);

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Doctor obtener(int idDoctor) throws DAOExcepcion {
		Doctor vo = new Doctor();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idDoctor,Usuarios_idusuario,dsApellido,dsNombre,dsCorreo,nuTeleFijo,nuTeleMovil from Doctor where idDoctor=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idDoctor);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdDoctor(rs.getInt(1));
				vo.setUsuarios_idusuario(rs.getInt(2));
				vo.setDsApellido(rs.getString(3));
				vo.setDsNombre(rs.getString(4));
				vo.setDsCorreo(rs.getString(5));
				vo.setNuTeleFijo(rs.getString(6));
				vo.setNuTeleMovil(rs.getString(7));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int idDoctor) throws DAOExcepcion {
		String query = "update Doctor Set stActivo = 'N' WHERE idDoctor=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idDoctor);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Doctor actualizar(Doctor vo) throws DAOExcepcion {
		String query = "update Doctor set Usuarios_idusuario=?,dsApellido=?,dsNombre=?,dsCorreo=?,nuTeleFijo=?,nuTeleMovil=? where idDoctor=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, vo.getUsuarios_idusuario());
			stmt.setString(2, vo.getDsApellido());
			stmt.setString(3, vo.getDsNombre());
			stmt.setString(4, vo.getDsCorreo());
			stmt.setString(5, vo.getNuTeleFijo());
			stmt.setString(6, vo.getNuTeleMovil());
			stmt.setInt(7, vo.getIdDoctor());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Collection<Doctor> listar() throws DAOExcepcion {
		Collection<Doctor> c = new ArrayList<Doctor>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idDoctor,Usuarios_idusuario,dsApellido,dsNombre,dsCorreo,nuTeleFijo,nuTeleMovil from Doctor order by dsNombre";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Doctor vo = new Doctor();
				vo.setIdDoctor(rs.getInt("idDoctor"));
				vo.setUsuarios_idusuario(rs.getInt("usuarios_idusuario"));
				vo.setDsApellido(rs.getString("dsApellido"));
				vo.setDsNombre(rs.getString("dsNombre"));
				vo.setDsCorreo(rs.getString("dsCorreo"));
				vo.setNuTeleFijo(rs.getString("nuTeleFijo"));
				vo.setNuTeleMovil(rs.getString("nuTeleMovil"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
}
